using lwzlz.Application.System;
using lwzlz.Application.System.Dto;
using lwzlz.Common.QR;
using lwzlz.Web.Areas.SysAdmin.Models;
using lwzlz.Web.Filter;
using System;
using System.Collections.Generic;
using System.IO;
using System.Web;
using System.Web.Configuration;
using System.Web.Mvc;

namespace lwzlz.Web.Areas.SysAdmin.Controllers
{
    /// <summary>
    /// 
    /// </summary>
    [MenuFilter]
    public class FontController : AdminController
    {
        /// <summary>
        /// 视频视图
        /// </summary>
        /// <returns></returns>
        public ActionResult Index()
        {
            return View();
        }

        /// <summary>
        /// 获取首页列表数据
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [MenuRoute("Index")]
        public JsonResult GetListPage()
        {
            LayTableModel result = new LayTableModel();
            FontService service = new FontService();
            try
            {
                int page = int.Parse(Request.Params["page"]);//当前页码
                int limit = int.Parse(Request.Params["limit"]);//每页数量
                string field = Request.Params["field"];//排序字段名
                string order = Request.Params["order"];//排序方式：desc, asc, null(空对象，默认排序)
                //参数
                Dictionary<string, string> dic = new Dictionary<string, string>();
                string[] par = { "Name", "Type" };

                foreach (string item in par)
                {
                    if (!string.IsNullOrWhiteSpace(Request.Params[item]))
                    {
                        dic.Add(item, Request.Params[item]);
                    }
                }
                int count = 0;

                var list = service.GetListPage(page, limit, dic, field, order, out count);

                result.data = list;
                result.count = count;

            }
            catch (Exception ex)
            {
                result.code = -1;
                result.msg = ex.Message;
            }
            return Json(result);
        }

        /// <summary>
        /// 音频视图
        /// </summary>
        /// <returns></returns>
        public ActionResult Audio()
        {
            return View();
        }

        /// <summary>
        /// 图片视图
        /// </summary>
        /// <returns></returns>
        public ActionResult Image()
        {
            return View();
        }

        /// <summary>
        /// 生成字体地址
        /// </summary>
        /// <returns></returns>
        public ActionResult Calligraphy()
        {
            var strCode = WebConfigurationManager.AppSettings["DomainName"] + "Study/Calligraphy/Index";
            var date = DateTime.Now;
            var folder = Path.Combine("upload", "Calligraphy", date.Year.ToString(), date.Month.ToString());
            var qrUrl = QRCode.OrdinaryQR(strCode, folder, "Calligraphy.png");
            ViewBag.QRUrl = qrUrl;
            return View();
        }

        /// <summary>
        /// 添加修改视图
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        [MenuRoute("Index")]
        public ActionResult AddOrEdit(int Id = 0, int Type = 0)
        {
            FontOutput model = null;
            FontService service = new FontService();
            if (Id > 0)
            {
                model = service.Get<FontOutput>(Id);
            }
            else
            {
                model = new FontOutput()
                {
                    Type = Type
                };
            }
            return View(model);
        }

        /// <summary>
        /// 查看视图
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        [MenuRoute("Index")]
        public ActionResult Look(int Id = 0)
        {
            FontOutput model = null;
            FontService service = new FontService();
            if (Id > 0)
            {
                model = service.Get<FontOutput>(Id);
            }
            else
            {
                model = new FontOutput();
            }
            return View(model);
        }

        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        [HttpPost]
        [ValidateAntiForgeryToken]
        public JsonResult Add(FontInput model)
        {
            ResponseModel response = new ResponseModel();
            try
            {
                int result = new FontService().Add(model);
                if (result > 0)
                {
                    response.Success = true;
                    response.Message = "添加成功";
                }
                else
                {
                    response.Success = false;
                    response.Message = "添加失败";
                }

            }
            catch (Exception ex)
            {
                response.Success = false;
                response.Message = ex.Message;
            }
            return Json(response, JsonRequestBehavior.DenyGet);
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        [ValidateAntiForgeryToken]
        public JsonResult Edit(FontInput model)
        {
            ResponseModel response = new ResponseModel();
            try
            {
                int id = new FontService().Edit(model);
                response.Success = true;
                response.Message = "修改成功";
            }
            catch (Exception ex)
            {
                response.Success = false;
                response.Message = ex.Message;
            }
            return Json(response, JsonRequestBehavior.DenyGet);
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="IdList"></param>
        /// <returns></returns>
        [MenuRoute("Delete")]
        public JsonResult DeleteList(string IdList)
        {
            ResponseModel response = new ResponseModel();
            try
            {
                new FontService().DeleteList(IdList);
                response.Success = true;
                response.Message = "删除成功";
            }
            catch (Exception ex)
            {
                response.Success = false;
                response.Message = ex.Message;
            }
            return Json(response, JsonRequestBehavior.DenyGet);
        }

        //获取二维码
        [MenuRoute("GetUrl")]
        public JsonResult GetUrl(int ID)
        {
            ResponseModel response = new ResponseModel();
            try
            {
                var font = new FontService().Get(ID);
                if (font != null)
                {
                    response.Success = true;
                    response.Message = "获取成功";
                    response.Data = "..\\..\\" + font.QRCode;
                }
                else
                {
                    throw new Exception("查询失败");
                }
            }
            catch (Exception ex)
            {
                response.Success = false;
                response.Message = ex.Message;
            }
            return Json(response, JsonRequestBehavior.DenyGet);
        }

        /// <summary>
        /// 重新生成二维码
        /// </summary>
        /// <returns></returns>
        [MenuRoute("Index")]
        public JsonResult AgainQrCode()
        {
            ResponseModel response = new ResponseModel();
            try
            {
                var list = new FontService().GetList();
                if (list != null && list.Count > 0)
                {
                    foreach (var item in list)
                    {
                        var strCode = WebConfigurationManager.AppSettings["DomainName"] + "Study/Font/Detail?id=" + HttpUtility.UrlEncode(new Common.Symmetric().Encrypto(item.Id.ToString()));
                        var date = DateTime.Now;
                        var folder = Path.Combine("upload", "font", date.Year.ToString(), date.Month.ToString());
                        var qrUrl = QRCode.OrdinaryQR(item.Id, strCode, folder, "");
                        item.QRCode = qrUrl;

                        new FontService().Update(item, null);
                    }

                    response.Success = true;
                    response.Message = "生成成功";
                }
                else
                {
                    throw new Exception("查询失败");
                }
            }
            catch (Exception ex)
            {
                response.Success = false;
                response.Message = ex.Message;
            }
            return Json(response, JsonRequestBehavior.DenyGet);
        }

    }
}
